function [jaco]=gen_j(un, lambda, nx, ny)
hx = 1/nx;
hy = 1/ny;
length = (nx+1)*(ny+1);

jaco = zeros(length);

for k=1:length
    [i, j] = ij(k,nx);  
    if (i==1)|(i==nx+1)|(j==1)|(j==ny+1) %if it is on the boundary, don't calculate
        jaco(k,k) =1;
    else

        if j < ny+1        %make sure it isn't the top row
            jaco(k, l(i,j+1,nx)) = 1/hy^2;   
        end
        if i < nx+1             % make sure it isn't the right side
            jaco(k, l(i+1,j,nx)) = 1/hx^2;
        end

        jaco(k,k) = -2/hx^2 -2/hy^2 +lambda*(1 + 2*un(k));

        if 1 < i                % make sure it isn't the left side
            jaco(k, l(i-1,j, nx)) = 1/hx^2;
        end
        if 1 < j                % make sure it isn't the bottom row
            jaco(k, l(i,j-1,nx)) = 1/hy^2;
        end
    end

end


